1
|
|
|
'use strict'; |
2
|
|
|
const Post = require('./Post'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @type {Class} |
6
|
|
|
* @property {number} forumId |
7
|
|
|
* @property {string} forumTitle |
8
|
|
|
* @property {number} id |
9
|
|
|
* @property {string} title |
10
|
|
|
* @property {Post[]} posts |
11
|
|
|
*/ |
12
|
|
|
class Thread { |
13
|
|
|
/** |
14
|
|
|
* @typedef {Object} RawThreadData |
15
|
|
|
* @property {Object} thread |
16
|
|
|
* @property {string} thread.forumid |
17
|
|
|
* @property {string} thread.forumtitle |
18
|
|
|
* @property {string} thread.threadid |
19
|
|
|
* @property {string} thread.title |
20
|
|
|
* @property {string} thread.threadtitle |
21
|
|
|
* @property {RawPostData[]} postbits |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param {RawThreadData} rawData |
26
|
|
|
*/ |
27
|
|
|
constructor(rawData) { |
28
|
|
|
this.rawData = rawData; |
29
|
|
|
this.__parseData(); |
30
|
|
|
this.__cleanup(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @private |
36
|
|
|
*/ |
37
|
|
|
__parseData() { |
38
|
|
|
if (this.rawData) { |
39
|
|
|
//TODO need to specify if its fully fetched |
40
|
|
|
let rawData = this.rawData; |
41
|
|
|
|
42
|
|
|
if (rawData.hasOwnProperty('thread')) { |
43
|
|
|
let threadData = rawData['thread']; |
44
|
|
|
if (threadData.hasOwnProperty('forumid')) { |
45
|
|
|
this.forumId = parseInt(threadData.forumid); |
46
|
|
|
} |
47
|
|
|
if (threadData.hasOwnProperty('forumtitle')) { |
48
|
|
|
this.forumTitle = threadData.forumtitle; |
49
|
|
|
} |
50
|
|
|
if (threadData.hasOwnProperty('threadid')) { |
51
|
|
|
this.id = parseInt(threadData.threadid); |
52
|
|
|
} |
53
|
|
|
if (threadData.hasOwnProperty('title')) { |
54
|
|
|
this.title = threadData.title; |
55
|
|
|
} else if (threadData.hasOwnProperty('threadtitle')) { |
56
|
|
|
this.title = threadData.threadtitle; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (rawData.hasOwnProperty('postbits')) { |
61
|
|
|
let postBits = rawData.postbits; |
62
|
|
|
this.posts = []; |
63
|
|
|
for (let post in postBits) { |
64
|
|
|
if (postBits.hasOwnProperty(post)) { |
65
|
|
|
this.posts.push(new Post(postBits[post])); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @private |
75
|
|
|
*/ |
76
|
|
|
__cleanup() { |
77
|
|
|
delete (this.rawData); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Attempts to submit a new Thread into a specified Forum. This will also be considered the first Post |
82
|
|
|
* @param {VBApi} VBApi |
83
|
|
|
* @param {number} forumId - Forum Id |
84
|
|
|
* @param {string} subject - Post/Thread Subject |
85
|
|
|
* @param {string} message - Post Message |
86
|
|
|
* @param {object=} options |
87
|
|
|
* @param {boolean=} options.signature - Optionally append your signature |
88
|
|
|
* @param {number=} options.forumid - Ignore, already required at postId |
89
|
|
|
* @param {string=} options.subject - Ignore, already required at postId |
90
|
|
|
* @param {string=} options.message - Ignore, already required at postId |
91
|
|
|
* TODO note additional options |
92
|
|
|
* @returns {Promise<*>} - Returns a unhandled response currently |
93
|
|
|
* @fulfill {*} |
94
|
|
|
* @reject {string} - Error Reason. Expects: (TODO list common errors here) |
95
|
|
|
*/ |
96
|
|
|
static async newThread(VBApi, forumId, subject, message, options) { |
97
|
|
|
let that = VBApi; |
98
|
|
|
options = options || {}; |
99
|
|
|
options.forumid = forumId || options.forumid || ''; //required |
100
|
|
|
options.subject = subject || options.subject || ''; //required |
101
|
|
|
options.message = message || options.message || ''; //required |
102
|
|
|
|
103
|
|
|
if (options.signature === true) { |
104
|
|
|
//System only handle 1 or 0. defaults to 0 |
105
|
|
|
options.signature = '1'; // FIXME This didn't seem to work |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return new Promise(async function (resolve, reject) { |
109
|
|
|
try { |
110
|
|
|
let response = await that.callMethod({ |
111
|
|
|
method: 'newthread_postthread', |
112
|
|
|
params: options |
113
|
|
|
}); |
114
|
|
|
let possibleError = that.constructor.parseErrorMessage(response); |
115
|
|
|
//success is errormessgae 'redirect_postthanks' |
116
|
|
|
//reports threadid and postid |
117
|
|
|
if ( |
118
|
|
|
possibleError === 'redirect_postthanks' |
119
|
|
|
&& response.hasOwnProperty('show') |
120
|
|
|
) { |
121
|
|
|
resolve(response.show); |
122
|
|
|
} else { |
123
|
|
|
reject(possibleError || response); |
124
|
|
|
} |
125
|
|
|
} catch (e) { |
126
|
|
|
reject(e); |
127
|
|
|
} |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* List detailed information about a Thread and it's Posts |
133
|
|
|
* @param {VBApi} VBApi |
134
|
|
|
* @param {number} threadId - Thread id |
135
|
|
|
* @param {object=} options - Secondary Options |
136
|
|
|
* @param {number=} options.threadid - Ignore, already required at threadId |
137
|
|
|
* TODO note additional options |
138
|
|
|
* @returns {Promise<Thread>} - Returns a Thread object |
139
|
|
|
* @fulfill {Thread} |
140
|
|
|
* @reject {string} - Error Reason. Expects: (TODO list common errors here) |
141
|
|
|
*/ |
142
|
|
|
static async getThread(VBApi, threadId, options) { |
143
|
|
|
let that = VBApi; |
144
|
|
|
options = options || {}; |
145
|
|
|
options.threadid = threadId || options.threadid || ''; //required |
146
|
|
|
|
147
|
|
|
return new Promise(async function (resolve, reject) { |
148
|
|
|
let thread = null; |
149
|
|
|
try { |
150
|
|
|
let response = await that.callMethod({ |
151
|
|
|
method: 'showthread', |
152
|
|
|
params: options |
153
|
|
|
}); |
154
|
|
|
if ( |
155
|
|
|
response |
156
|
|
|
&& response.hasOwnProperty('response') |
157
|
|
|
) { |
158
|
|
|
thread = new Thread(response.response); |
159
|
|
|
} |
160
|
|
|
} catch (e) { |
161
|
|
|
reject(e); |
162
|
|
|
} |
163
|
|
|
if (thread !== null) { |
164
|
|
|
resolve(thread); |
165
|
|
|
} else { |
166
|
|
|
reject(); |
167
|
|
|
} |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* TODO incomplete - does not seem to function yet |
173
|
|
|
* Attempts to close a specific Thread. Requires a user to have a 'inline mod' permissions |
174
|
|
|
* @param {VBApi} VBApi |
175
|
|
|
* @param {number} threadId - Id of Thread to close |
176
|
|
|
* @returns {Promise<*>} - Returns a unhandled response currently |
177
|
|
|
* @fulfill {*} |
178
|
|
|
* @reject {string} - Error Reason. Expects: (TODO list common errors here) |
179
|
|
|
*/ |
180
|
|
|
static async closeThread(VBApi, threadId) { |
181
|
|
|
let that = VBApi; |
182
|
|
|
let cookies = {}; |
183
|
|
|
if (threadId) { |
184
|
|
|
//TODO multiple ids are delimited with a '-'. eg: 123-345-456 |
185
|
|
|
cookies.vbulletin_inlinethread = threadId; |
186
|
|
|
} |
187
|
|
|
return new Promise(async function (resolve, reject) { |
188
|
|
|
try { |
189
|
|
|
let response = await that.callMethod({ |
190
|
|
|
method: 'inlinemod_close', |
191
|
|
|
cookies: cookies || {} |
192
|
|
|
}); |
193
|
|
|
//let possibleError = that.constructor.parseErrorMessage(response); |
194
|
|
|
//unknown responses |
195
|
|
|
/*if ( |
196
|
|
|
possibleError === 'redirect_postthanks' |
197
|
|
|
&& response.hasOwnProperty('show') |
198
|
|
|
) {*/ |
199
|
|
|
resolve(response); |
200
|
|
|
/*} else { |
201
|
|
|
reject(possibleError || response); |
202
|
|
|
}*/ |
203
|
|
|
} catch (e) { |
204
|
|
|
reject(e); |
205
|
|
|
} |
206
|
|
|
}); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* TODO incomplete - does not seem to function yet |
211
|
|
|
* Attempts to open a specific Thread. Requires a user to have a 'inline mod' permissions |
212
|
|
|
* @param {VBApi} VBApi |
213
|
|
|
* @param {number} threadId - Id of Thread to open |
214
|
|
|
* @returns {Promise<*>} - Returns a unhandled response currently |
215
|
|
|
* @fulfill {*} |
216
|
|
|
* @reject {string} - Error Reason. Expects: (TODO list common errors here) |
217
|
|
|
*/ |
218
|
|
|
static async openThread(VBApi, threadId) { |
219
|
|
|
let that = VBApi; |
220
|
|
|
let cookies = {}; |
221
|
|
|
if (threadId) { |
222
|
|
|
//TODO multiple ids are delimited with a '-'. eg: 123-345-456 |
223
|
|
|
cookies.vbulletin_inlinethread = threadId; |
224
|
|
|
} |
225
|
|
|
return new Promise(async function (resolve, reject) { |
226
|
|
|
try { |
227
|
|
|
let response = await that.callMethod({ |
228
|
|
|
method: 'inlinemod_open', |
229
|
|
|
cookies: cookies || {} |
230
|
|
|
}); |
231
|
|
|
//let possibleError = that.constructor.parseErrorMessage(response); |
232
|
|
|
//unknown responses |
233
|
|
|
/*if ( |
234
|
|
|
possibleError === 'redirect_postthanks' |
235
|
|
|
&& response.hasOwnProperty('show') |
236
|
|
|
) {*/ |
237
|
|
|
resolve(response); |
238
|
|
|
/*} else { |
239
|
|
|
reject(possibleError || response); |
240
|
|
|
}*/ |
241
|
|
|
} catch (e) { |
242
|
|
|
reject(e); |
243
|
|
|
} |
244
|
|
|
}); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* TODO incomplete - does not seem to function yet |
249
|
|
|
* Attempts to delete a specific Thread. Requires a user to have a 'inline mod' permissions |
250
|
|
|
* @param {VBApi} VBApi |
251
|
|
|
* @param {number} threadId - Id of Thread to close |
252
|
|
|
* @returns {Promise<*>} - Returns a unhandled response currently |
253
|
|
|
* @fulfill {*} |
254
|
|
|
* @reject {string} - Error Reason. Expects: (TODO list common errors here) |
255
|
|
|
*/ |
256
|
|
|
static async deleteThread(VBApi, threadId) { |
257
|
|
|
let that = VBApi; |
258
|
|
|
let cookies = {}; |
259
|
|
|
if (threadId) { |
260
|
|
|
//TODO multiple ids are delimited with a '-'. eg: 123-345-456 |
261
|
|
|
cookies.vbulletin_inlinethread = threadId; |
262
|
|
|
} |
263
|
|
|
return new Promise(async function (resolve, reject) { |
264
|
|
|
try { |
265
|
|
|
let response = await that.callMethod({ |
266
|
|
|
method: 'inlinemod_dodeletethreads', |
267
|
|
|
cookies: cookies || {} |
268
|
|
|
}); |
269
|
|
|
//let possibleError = that.constructor.parseErrorMessage(response); |
270
|
|
|
//unknown responses |
271
|
|
|
/*if ( |
272
|
|
|
possibleError === 'redirect_postthanks' |
273
|
|
|
&& response.hasOwnProperty('show') |
274
|
|
|
) {*/ |
275
|
|
|
resolve(response); |
276
|
|
|
/*} else { |
277
|
|
|
reject(possibleError || response); |
278
|
|
|
}*/ |
279
|
|
|
} catch (e) { |
280
|
|
|
reject(e); |
281
|
|
|
} |
282
|
|
|
}); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
module.exports = Thread; |